{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import symforce\n",
    "\n",
    "symforce.set_symbolic_api(\"sympy\")\n",
    "\n",
    "import symforce.symbolic as sf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define some variables\n",
    "M = sf.M22.symbolic(\"M\")\n",
    "R = sf.Rot2.symbolic(\"R\")\n",
    "lmbda = sf.Symbol(\"\\lambda\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the lagrangian\n",
    "# Minimize the frobenius norm, with the constraint that R.z is unit norm\n",
    "L = (M - R.to_rotation_matrix()).reshape(4, 1).squared_norm() + lmbda * (R.z.squared_norm() - 1)\n",
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "L.diff(R.z.real)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "L.diff(R.z.imag)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "L.diff(lmbda)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Compute extremum points\n",
    "S0, S1 = sf.solve([L.diff(R.z.real), L.diff(R.z.imag), L.diff(lmbda)], [R.z.real, R.z.imag, lmbda])\n",
    "display(S0)\n",
    "display(S1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Check if the extremum points are minima - first compute the symbolic hessian\n",
    "H = sf.V1(L).jacobian(sf.V3(R.z.real, R.z.imag, lmbda)).jacobian(sf.V3(R.z.real, R.z.imag, lmbda))\n",
    "H.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The first solution is a local maximum\n",
    "H.det().subs({R.z.real: S0[0], R.z.imag: S0[1], lmbda: S0[2]}).simplify()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# And the second is a minimum\n",
    "H.det().subs({R.z.real: S1[0], R.z.imag: S1[1], lmbda: S1[2]}).simplify()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "display(S1[0])\n",
    "display(S1[1])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
